﻿using System;
using BalanceManager.ViewModels;

namespace BalanceManager.Helpers
{
    public static class DateUtils
    {
        public static void GetStartAndEndDateByPeriod(DateTime date, Period period, out DateTime startDate, out DateTime endDate)
        {
            startDate = date;
            endDate = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
            switch (period)
            {
                case Period.Today:
                    startDate = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
                    break;

                case Period.Yesterday:
                    startDate = date.AddDays(-1);
                    startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
                    break;

                case Period.Week:
                    startDate = GetBegginingOfWeekForDate(date);
                    startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
                    break;

                case Period.Month:
                    startDate = new DateTime(date.Year, date.Month, 1, 0, 0, 0);
                    break;

                case Period.Year:
                    startDate = new DateTime(date.Year, 1, 1, 0, 0, 0);
                    break;
            }
        }

        public static DateTime GetBegginingOfWeekForDate(DateTime date)
        {
            while (date.DayOfWeek != DayOfWeek.Monday)
                date = date.AddDays(-1);
            return date;
        }
    }
}
